<?php
// 首页显示信息
namespace NewAdmin\Controller;
use Think\Controller;
class CommonController extends Controller {
    var $userArray  =   array();
    var $jsonArray =   array();
	function _initialize(){
		if( !session('user_id') ){
            $this->assign('title','用户登录');
            $this->display('User:login2');
            die();
		}

        $menu   =   D('Menu');
        $menuArray  =   $menu->where('bclassid = 0 AND type = 0 AND ishidden = 0')->order('sort ASC')->select();
        if ($menuArray ){
        	foreach ($menuArray AS $key=>$row ){
                $menuChildrenArray  =   array();
                $menuChildrenArray  =   $menu->where(' bclassid = '.$row['id'].' AND type = 0 AND ishidden = 0')->order('sort ASC')->select();
                $menuArray[$key]['sub'] =   $menuChildrenArray;
        	}
        }
        $this->assign('menuArray',$menuArray);
        $menuArraySub  =   $menu->where(' module = "'.CONTROLLER_NAME.'" AND bclassid <> "0" AND type = 0 AND ishidden = 0')->order('sort ASC')->select();
        $this->assign('menuArraySub',$menuArraySub);

        //获取当前用户信息
        $user   =   D('User');
        $userArray  =   $user->where('ishidden = 0 AND id = '.session('user_id') )->find();
        $this->userArray    =   $userArray;
        $this->assign('userArray',$userArray);
        $c_name = CONTROLLER_NAME;
        $a_name =  ACTION_NAME;
        $c_name=strtolower($c_name);
        $a_name=strtolower($a_name);

        //验证是否有权限查看当前栏目
        if($userArray['user_level']!=0){
            $rbac_ids=M('rbac')->where('type=1 and uid='.session('user_id'))->find();
            if($rbac_ids){
                $menus  =   $menu->where('type = 0')->order('sort ASC')->select();
                foreach ($menus as $key=>$val){
                    $module=strtolower($val['module']);
                    $action=strtolower($val['action']);
                    if($c_name==$module&&$action==$a_name){
                        $rbac_id=$val['id'];
                    }
                }
                if($rbac_id){
                    $bids=explode(',',$rbac_ids['bids']);
                    if(!in_array($rbac_id,$bids)){
                        $this->error('当前帐号无权限访问该模块','/');
                    }
                }
            }else{
                if($c_name!='index'||$a_name!='index'){
                    $this->error('当前帐号未分配权限','/');
                }
            }
        }





        $fromType	=	getFromType();
        $this->fromType	=	$fromType;
        $this->assign('fromType',$fromType);
	}

	
	
    function urlto($flag = true,$url = ''){
        $url    =   $url?'location.href="'.$url.'";':'history.back(0);';
        if( $flag !== false ){
            echo '<script type="text/javascript">alert("操作成功");'.$url.'</script>';
        }else{
            echo '<script type="text/javascript">alert("操作失败");'.$url.'</script>';
        }
    }

	function uploadPic($file,$width = '',$height = ''){
		//导入上传类
		$upload = new \Think\Upload();
		//设置上传文件大小
		$upload->maxSize = 3292200;
		//设置上传文件类型
		$upload->exts = array('jpg','gif','png','jpeg');
		//设置附件上传目录
		$upload->rootPath = '../Uploads/';
		$uploadList   =   $upload->uploadOne($file);
        $image = '/Uploads/'.$uploadList['savepath'].$uploadList['savename'];
        $name	=	$_SERVER["DOCUMENT_ROOT"].'/Uploads/'.$uploadList['savepath'].$uploadList['savename'];
        $imageArray	=	getimagesize($name);
        if( ( $width || $height ) && ( $imageArray[0] > $width || $imageArray[1] > $height ) ){
	        $image1 = new \Think\Image();
			$image1->open($name);
			// 生成一个居中裁剪为150*150的缩略图并保存为thumb.jpg
			$image1->thumb($width,$height,\Think\Image::IMAGE_THUMB_CENTER)->save($name);
        }
		return $image;
    }

    public function ajax_r($status='0',$message='',$data=array()) {
        $msg['status']=$status;
        $msg['data']=$data;
        $msg['message']=$message;
        $this->ajaxReturn($msg);
    }

}
?>